<template>
  <div class="home-container">
    <!-- 导航栏开始 -->
    <van-nav-bar class="page-bav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 导航栏结束 -->

    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="obj in userChannels" :key="obj.id">
        <!-- 导航 -->
        <div slot="title">
          {{ obj.name }}
        </div>

        <!-- 内容 -->
        <div slot="default">
          <ArticleList :channel="obj"></ArticleList>
        </div>
      </van-tab>
      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <div class="placeholder" slot="nav-right"></div>
      <!-- 右侧按钮 -->
      <div class="hamburger-btn" slot="nav-right" @click="isShowPopup = true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!--  -->
    <van-popup
      class="edit-channel-popup"
      v-model="isShowPopup"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
      ><ChannelEdit
        :myChannels="userChannels"
        :active="active"
        @uodata-active="onUodataActive"
      ></ChannelEdit
    ></van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from '@/views/home/components/article-list.vue'
import ChannelEdit from './components/channel-edit'
import { mapState } from 'vuex'
import { getItem } from '../../utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      userChannels: [],
      isShowPopup: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadUserChannels()
  },
  mounted () {},
  methods: {
    async loadUserChannels () {
      // 判断用户有没有登录
      if (this.user) {
        // 登录了
        try {
          const res = await getUserChannels()
          // console.log(res);
          this.userChannels = res.data.data.channels
        } catch (error) {
          console.log(error)
        }
      } else {
        // 未登录
        const loadChannel = getItem('totiao_KEY')
        // 再判断 本地有没有频道数据
        if (loadChannel) {
          // 有数据 直接渲染
          this.userChannels = loadChannel
        } else {
          // 没有数据 请求默认数据
          try {
            const res = await getUserChannels()
            // console.log(res);
            this.userChannels = res.data.data.channels
          } catch (error) {
            console.log(error)
          }
        }
      }
    },
    onUodataActive (ind, bool) {
      this.active = ind // 跳到该频道
      this.isShowPopup = bool // 关闭弹出层
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  // tabs 标签导航也设置为固定定位
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
    height: 82px;
  }

  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
